package com.hxzy.controller;

import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author gityyge
 * @Description 发送异步消息
 * @Date 2023/8/9 17:09
 * @Version 1.0
 */
@Log4j2
@RestController
public class ProducerAsyncMessageController {

    @Autowired
    private RocketMQTemplate  rocketMQTemplate;

    /**
     *  发送异步消息
     * @param message
     * @return
     */

    @PostMapping(value = "/send/async")
    public void sendMessage03(String message){
        //1、创建一个消息
        Message<String> messageObj = MessageBuilder.withPayload(message).build();
        //2、向rocketmq发送消息
        // 目的地  destination – formats: `topicName:tags`    主题名称：标识(过滤字符)
        // 消息对象    Message
        // 消息内容对象
        this.rocketMQTemplate.asyncSend("hxzy_test:A", messageObj, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
               //成功做什么事情
                log.info(sendResult.toString());
            }

            @Override
            public void onException(Throwable throwable) {
                //失败了记录
                log.error(throwable);
                //补偿（存储数据库，redis发送失败的内容）
            }
        });
    }


}
